package com.ajie.bluetoothcard.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;
import com.ajie.bluetoothcard.domain.Card;
import com.ajie.bluetoothcard.ui.CardApplication;

import android.util.Xml;

public class CardModelsXml {
	
	/**
	 * 获取数据
	 * @param 传入一个输入流，不管是读取内置的还是后来存储到手机上的
	 * @return
	 * @throws Exception
	 */
	public static Card getMobileModels(InputStream xml) throws Exception{
		Card card = null;
		XmlPullParser pullParser = Xml.newPullParser();
		pullParser.setInput(xml, "UTF-8");//为Pull解析器设置要解析的XML数据
		int event = pullParser.getEventType();
		while(event != XmlPullParser.END_DOCUMENT){
			switch (event) {
			case XmlPullParser.START_DOCUMENT:
				break;
			case XmlPullParser.START_TAG:
				if("card".equals(pullParser.getName())){
					card = new Card();
				}
				if("name".equals(pullParser.getName())){
					String name = pullParser.nextText();
					card.setName(name);
				}
				if("mobilePhone".equals(pullParser.getName())){
					String mobilePhone = pullParser.nextText();
					card.setMobilePhone(mobilePhone);
				}
				if("telephone".equals(pullParser.getName())){
					String telephone = pullParser.nextText();
					card.setTelephone(telephone);
				}  
				if("avatar".equals(pullParser.getName())){
					String telephone = pullParser.nextText();
					card.setTelephone(telephone);
				}
				if("title".equals(pullParser.getName())){
					String avatar = pullParser.nextText();
					card.setAvatar(avatar);
				}
				if("company".equals(pullParser.getName())){
					String company = pullParser.nextText();
					card.setCompany(company);
				}
				if("voide".equals(pullParser.getName())){
					String voide = pullParser.nextText();
					card.setVoide(voide);
				}
				if("address".equals(pullParser.getName())){
					String address = pullParser.nextText();
					card.setAddress(address);
				}
				if("introduction".equals(pullParser.getName())){
					String introduction = pullParser.nextText();
					card.setIntroduction(introduction);
				}
				if("type".equals(pullParser.getName())){
					String type = pullParser.nextText();
					card.setType(type);
				}
				if("url".equals(pullParser.getName())){
					String url = pullParser.nextText();
					card.setUrl(url);
				}
				if("email".equals(pullParser.getName())){
					String email = pullParser.nextText();
					card.setEmail(email);
				}
				if("fax".equals(pullParser.getName())){
					String fax = pullParser.nextText();
					card.setFax(fax);
				}
				break;
			case XmlPullParser.END_TAG:
				break;
			}
			event = pullParser.next();
		}
		return card;
	}
	
	/**
	 * 保存XML信息
	 * @param mobileModels
	 * @return false:保存失败
	 * 				   true：保存成功
	 */
	public static boolean saveMobileToXml(Card card){
		try {
			XmlSerializer  serializer =  Xml.newSerializer();
			File file = new File(CardApplication.getRootDir(),"card.xml");
			FileOutputStream fos = new FileOutputStream(file);
			serializer.setOutput(fos, "utf-8");
			serializer.startDocument("utf-8", true);
				serializer.startTag(null, "card");
				
				serializer.startTag(null,"name");
				serializer.text(card.getName());
				serializer.endTag(null, "name");
				
				serializer.startTag(null,"mobilePhone");
				serializer.text(card.getMobilePhone());
				serializer.endTag(null, "mobilePhone");
				
				serializer.startTag(null,"telephone");
				serializer.text(card.getTelephone());
				serializer.endTag(null, "telephone");
				
				serializer.startTag(null,"avatar");
				serializer.text(card.getAvatar());
				serializer.endTag(null, "avatar");
				
				serializer.startTag(null,"title");
				serializer.text(card.getTitle());
				serializer.endTag(null, "title");
				
				serializer.startTag(null,"company");
				serializer.text(card.getCompany());
				serializer.endTag(null, "company");
				
				serializer.startTag(null,"voide");
				serializer.text(card.getVoide());
				serializer.endTag(null, "voide");
				
				serializer.startTag(null,"address");
				serializer.text(card.getAddress());
				serializer.endTag(null, "address");
				
				serializer.startTag(null,"introduction");
				serializer.text(card.getIntroduction());
				serializer.endTag(null, "introduction");
				
//				serializer.startTag(null,"time");
//				serializer.text(card.getTime());
//				serializer.endTag(null, "time");
				
				serializer.startTag(null,"type");
				serializer.text("1");// TODO 这里发送出去的类型都是 "1" 未分组
				serializer.endTag(null, "type");
				
//				serializer.startTag(null,"isDel");
//				serializer.text(card.getIsDel());
//				serializer.endTag(null, "isDel");
				
				serializer.startTag(null,"url");
				serializer.text(card.getUrl());
				serializer.endTag(null, "url");
				
				serializer.startTag(null,"email");
				serializer.text(card.getEmail());
				serializer.endTag(null, "email");
				
				serializer.startTag(null,"fax");
				serializer.text(card.getFax());
				serializer.endTag(null, "fax");
				
				serializer.endTag(null, "card");
			serializer.endDocument();
			fos.flush();
			fos.close();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
}
